2 Dimensional Local Histogram Equalization


In [59]:
#insert your own path here
import os
os.chdir('/Users/albertlee/NeuroCV/Module1/PCV/2Dhisteq')

In [22]:
### Run below if necessary

##import sys

##sys.path.append('/usr/local/lib/python2.7/site-packages')

import math
import csv,gc
import matplotlib
import numpy as np
import cv2

#%matplotlib
BINS = 32

In [23]:
import matplotlib.pyplot as plt
%matplotlib inline

In [24]:
from skimage import data, img_as_float
from skimage import exposure
import cv2

In [63]:
from PCV import tools
from PIL import Image
from numpy import *

In [64]:
im = array(Image.open('Fear199.png'))
plt.imshow(im)


Out[64]:
<matplotlib.image.AxesImage at 0x114c67c90>

In [75]:
im = cv2.imread('Fear199.png',0)
img = np.asarray(im)

In [76]:
imgflat = img.reshape(-1)
print imgflat.sum()

print " "
fig = plt.hist(imgflat, bins=255)
plt.title('Histogram')
plt.show()

print " "

#clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe = cv2.createCLAHE()

#img_grey = np.array(img * 255, dtype = np.uint8)
#threshed = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)

cl1 = clahe.apply(im)
 
#cv2.imwrite('clahe_2.jpg',cl1)
#cv2.startWindowThread()
#cv2.namedWindow("adaptive")
#cv2.imshow("adaptive", cl1)
#cv2.imshow("adaptive", threshed)
#plt.imshow(threshed)

print " "

localimgflat = cl1.reshape(-1)
print localimgflat
print localimgflat.sum()

print " "
fig = plt.hist(localimgflat, bins=255)
plt.title('Locally Equalized Histogram')
plt.show()


1337332
 
 
 
[41 41 41 ..., 41 41 41]
62295747
 

In [77]:
cv2.imwrite('AdaptiveFear199.jpg',cl1)
plt.imshow(cl1)


Out[77]:
<matplotlib.image.AxesImage at 0x11b339350>

In [78]:
image = array(Image.open('AdaptiveFear199.png'))
plt.imshow(np.asarray(image))


Out[78]:
<matplotlib.image.AxesImage at 0x118143510>

In [46]:
import scipy.misc
rgb = scipy.misc.toimage(cl1)
plt.imshow(cl1) ## This alternate cl1 was constructed by applying a gray filter over the existing filter


Out[46]:
<matplotlib.image.AxesImage at 0x114222ad0>

In [53]:
im = cv2.imread('adaptexample.png',0)
img = np.asarray(im)

In [69]:
imgflat = img.reshape(-1)
print imgflat.sum()

print " "
fig = plt.hist(imgflat, bins=255)
plt.title('Histogram')
plt.show()

print " "

#clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
clahe = cv2.createCLAHE()

#img_grey = np.array(img * 255, dtype = np.uint8)
#threshed = cv2.adaptiveThreshold(img_grey, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 3, 0)

cl1 = clahe.apply(im)
 
#cv2.imwrite('clahe_2.jpg',cl1)
#cv2.startWindowThread()
#cv2.namedWindow("adaptive")
#cv2.imshow("adaptive", cl1)
#cv2.imshow("adaptive", threshed)
#plt.imshow(threshed)

print " "

localimgflat = cl1.reshape(-1)
print localimgflat
print localimgflat.sum()

print " "
fig = plt.hist(localimgflat, bins=255)
plt.title('Locally Equalized Histogram')
plt.show()


10378503
 
 
 
[53 53 53 ..., 77 77 77]
35934672
 
Out[69]:
True

In [56]:
cv2.imwrite('clahe_2.jpg',cl1)
plt.imshow(cl1)


Out[56]:
<matplotlib.image.AxesImage at 0x114d1d5d0>

In [68]:
image = array(Image.open('adaptimagefinal.png'))
plt.imshow(np.asarray(image))


Out[68]:
<matplotlib.image.AxesImage at 0x11b46a7d0>

In [47]:
gray_image = cv2.cvtColor(cl1, cv2.COLOR_BGR2GRAY)
plt.imshow(gray_image)


---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-47-10901723b8d1> in <module>()
----> 1 gray_image = cv2.cvtColor(cl1, cv2.COLOR_BGR2GRAY)
      2 plt.imshow(gray_image)

error: /tmp/opencv-20160604-38092-6muj0l/opencv-2.4.13/modules/imgproc/src/color.cpp:3739: error: (-215) scn == 3 || scn == 4 in function cvtColor

In [37]:
from skimage import color
from skimage import io

img = color.rgb2gray(io.imread('Fear199.png'))
plt.imshow(img)


Out[37]:
<matplotlib.image.AxesImage at 0x1140c8850>

In [40]:
rgbimg = Image.new("RGBA", img.size)
newimg = rgbimg.paste(img)
plt.imshow(newimg)


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-40-1e747a08fd36> in <module>()
----> 1 rgbimg = Image.new("RGBA", img.size)
      2 newimg = rgbimg.paste(img)
      3 plt.imshow(newimg)

/usr/local/lib/python2.7/site-packages/PIL/Image.pyc in new(mode, size, color)
   2034         color = ImageColor.getcolor(color, mode)
   2035 
-> 2036     return Image()._new(core.fill(mode, size, color))
   2037 
   2038 

TypeError: must be 2-item sequence, not int

In [42]:
grey()


---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-42-ca9f256e8aea> in <module>()
----> 1 grey()

NameError: name 'grey' is not defined

In [ ]: